package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import edu.whu.demo.domain.Message;
import edu.whu.demo.domain.Reminder;
import edu.whu.demo.dao.ReminderDao;
import edu.whu.demo.domain.User;
import edu.whu.demo.exception.ReminderException;
import edu.whu.demo.service.IMessageService;
import edu.whu.demo.service.IReminderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-25
 */
@Service
public class ReminderServiceImpl extends ServiceImpl<ReminderDao, Reminder> implements IReminderService {

    @Autowired
    IMessageService messageService;

    @Override
    public IPage<Reminder> getRemindersByActivityId(Long activityId,
                                                    Integer pageNum,
                                                    Integer pageSize)
    {
        Page<Reminder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Reminder> qw = new QueryWrapper<>();
        qw.eq("activity_id",activityId);
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public Reminder addReminder(Reminder reminder) throws ReminderException
    {
        try
        {
            this.baseMapper.insert(reminder);

            Message message = new Message();
            message.setActivityId(reminder.getActivityId());
            message.setFromUid(reminder.getId());
            message.setContent(reminder.getContent());
            message.setTime(reminder.getTime());
            message.setType("reminder");
            message.setIsRead(0);
            messageService.addMessage(message);
        }
        catch (Exception e)
        {
            throw new ReminderException("添加提醒失败："+e.getMessage());
        }
        return reminder;
    }

    @Override
    public void updateReminder(Reminder reminder) throws ReminderException
    {
        try
        {
            this.baseMapper.updateById(reminder);

            //更新reminder之后需要同步更新message
            Message message = new Message();
            message.setContent(reminder.getContent());
            message.setTime(reminder.getTime());
            message.setType("reminder");
            message.setFromUid(reminder.getId());
            messageService.updateMessage(message);
        }
        catch (Exception e)
        {
            throw new ReminderException("修改提醒失败："+e.getMessage());
        }
    }

    @Override
    public void deleteReminder(Long id) throws ReminderException
    {
        try
        {
            this.baseMapper.deleteById(id);

            Message message = new Message();
            message.setType("reminder");
            message.setFromUid(id);
            messageService.deleteMessage(message);
        }
        catch (Exception e)
        {
            throw new ReminderException("删除提醒失败："+e.getMessage());
        }
    }

    @Override
    public List<Reminder> getAll(){
       return this.baseMapper.getAllReminders();
    }

}
